package com.example.naodian.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.naodian.context.BaseContext;
import com.example.naodian.domain.dto.RecordingDto;
import com.example.naodian.domain.po.EegRecording;
import com.example.naodian.domain.po.User;
import com.example.naodian.domain.vo.EegRecordingVO;
import com.example.naodian.mapper.EegRecordingMapper;
import com.example.naodian.result.Result;
import com.example.naodian.service.IEegRecordingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.naodian.service.IWarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.*;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2025-04-10
 */
@Service
public class EegRecordingServiceImpl extends ServiceImpl<EegRecordingMapper, EegRecording> implements IEegRecordingService {

    /**
     * 多条件查询记录信息
     * @param id 记录id
     * @param userId 用户id
     * @param recordingName 记录名称
     * @return EegRecordingVO集合
     */
    @Override
    public List<EegRecordingVO> getEegRecordingInfo(Long id, Long userId, String recordingName) {
        List<EegRecording> list = lambdaQuery()
                .eq(id != null, EegRecording::getId, id)
                .eq(userId != null, EegRecording::getUserId, userId)
                .eq(recordingName != null, EegRecording::getRecordingName, recordingName)
                .list();
        return BeanUtil.copyToList(list,EegRecordingVO.class);
    }

    /**
     * 新增记录信息
     * @param recordingDto
     * @return
     */
    @Override
    public Long add(RecordingDto recordingDto) {
        //属性拷贝
        EegRecording eegRecording = BeanUtil.copyProperties(recordingDto, EegRecording.class);

        //用户id
        eegRecording.setUserId(BaseContext.getCurrentId());

        //保存数据库
        save(eegRecording);

        //返回新增记录的id
        return eegRecording.getId();
    }


    /**
     * 更新时间
     * @param
     */
    @Override
    public void update(LocalDateTime startTime, LocalDateTime endTime) {
        LambdaUpdateWrapper<EegRecording> updateWrapper = new LambdaUpdateWrapper<>();

        // 如果 startTime 不为 null，则更新 startTime 字段
        if (startTime != null) {
            updateWrapper.set(EegRecording::getStartTime, startTime);
        }

        // 如果 endTime 不为 null，则更新 endTime 字段
        if (endTime != null) {
            updateWrapper.set(EegRecording::getEndTime, endTime);
        }

        // 执行更新操作
        update(updateWrapper);
    }
}
